package com.xteam.xnetworkdisk.sys.service.impl;

import com.xteam.xnetworkdisk.utils.PageUtils;
import com.xteam.xnetworkdisk.utils.Query;
import com.xteam.xnetworkdisk.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xteam.xnetworkdisk.sys.dao.LoginHistoryDao;
import com.xteam.xnetworkdisk.sys.entity.LoginHistoryEntity;
import com.xteam.xnetworkdisk.sys.service.LoginHistoryService;


@Service("loginHistoryService")
public class LoginHistoryServiceImpl extends ServiceImpl<LoginHistoryDao, LoginHistoryEntity> implements LoginHistoryService {

    @Autowired
    private IPService ipService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<LoginHistoryEntity> page = this.page(
                new Query<LoginHistoryEntity>().getPage(params),
                new QueryWrapper<LoginHistoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Boolean loginInsert(Integer userId, String ip, String location, int isSuccess,String device) {

        Date date = new Date();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println(date.toString());

        LoginHistoryEntity loginHistoryEntity = new LoginHistoryEntity();
        loginHistoryEntity.setUserId(userId);
        loginHistoryEntity.setSuccessful(isSuccess);
        loginHistoryEntity.setLocation(location);
        loginHistoryEntity.setIpAddress(ip);
        loginHistoryEntity.setLoginTime(date);
        loginHistoryEntity.setDeviceInfo(device);
        int i = this.baseMapper.insertLoginHistory(loginHistoryEntity);

        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public R getLoginHistory() {
        List<LoginHistoryEntity> loginHistory = this.baseMapper.selectList(new QueryWrapper<LoginHistoryEntity>().orderByDesc("login_time"));
        return R.ok().put("loginHistory", loginHistory);
    }

}